from backend.app.app.db.session_project import session_pro
import backend.app.app.models.db_owi_private_models as md_pro
import json

#根据被共享人的id获取共享人共享的用户id和项目id
def get_uid_proid():
    try:
        id = session_pro.query(
            md_pro.TbUserprojShareset.Proj_ID,
            md_pro.TbUserprojShareset.User_ID,
            md_pro.TbUserprojShareset.ProjShared_User
        ).all()
        session_pro.close()
        result = [dict(zip(data.keys(), data)) for data in id]
        return result
    except Exception as e:
        print("获取id失败",e,e.__traceback__.tb_lineno)
        session_pro.rollback()
        return False

#获取所有自己共享信息
def get_all_share_db(user_id,user_name):
    try:
        temp_list=list()
        result=list()
        db_data = session_pro.query(
            md_pro.TbUserprojShareset
        ).all()
        user={"ProjShared_UserID":user_id,"ProjShared_UserName":user_name}
        user = json.dumps(user).replace(' ','')
        if db_data:
            info = [data.__dict__ for data in db_data]
            for data in info:
                if user in data['ProjShared_User'].replace(' ',''):
                    temp_list.append(data)
        if len(temp_list):
            for data in temp_list:
                share_inf = session_pro.query(md_pro.TbUserprojShareset).filter(
                    md_pro.TbUserprojShareset.Proj_ID == data['Proj_ID'],
                    md_pro.TbUserprojShareset.User_ID == data['User_ID'],
                    md_pro.TbUserprojShareset.ProjShared_User == data['ProjShared_User']
                ).first()
                result.append(share_inf.__dict__)
        session_pro.close()
        return result
    except Exception as e:
        print('查找共享信息失败',e,e.__traceback__.tb_lineno)
        session_pro.rollback()
        return False

#新建共享信息
def new_share(project_id,project_name,user_id,user_name,share_user,ProjShared_PermitType,remark):
    try:
        share_user=share_user.replace(' ','')
        db_data = session_pro.query(md_pro.TbUserprojShareset).filter(
            md_pro.TbUserprojShareset.Proj_ID==project_id,
            md_pro.TbUserprojShareset.User_ID==user_id
        ).first()
        if db_data:
            session_pro.query(md_pro.TbUserprojShareset).filter(
                md_pro.TbUserprojShareset.Proj_ID==project_id,
                md_pro.TbUserprojShareset.User_ID==user_id
            ).update({
                'Proj_Name' : project_name,
                'User_Name' : user_name,
                'ProjShared_User' : share_user,
                'ProjShared_PermitType' : ProjShared_PermitType,
                'Remark' : remark
            })
        else:
            new_shar_inf = md_pro.TbUserprojShareset(
                Proj_ID=project_id,
                Proj_Name=project_name,
                User_ID=user_id,
                User_Name=user_name,
                ProjShared_User=share_user,
                ProjShared_PermitType=ProjShared_PermitType,
                Remark=remark
            )
            session_pro.add(new_shar_inf)
        session_pro.commit()
        print("新建共享信息成功")
        return True
    except Exception as e:
        print("新建共享信息失败",e,e.__traceback__.tb_lineno)
        session_pro.rollback()
        return False

def get_share_proj(proj_id,user_id):
    try:
        db_data = session_pro.query(md_pro.TbUserprojShareset).filter(
            md_pro.TbUserprojShareset.Proj_ID == proj_id,
            md_pro.TbUserprojShareset.User_ID == user_id
        ).first()
        if db_data:
            return db_data.__dict__
        else:
            return None
    except Exception as e:
        print("获取共享信息失败", e, e.__traceback__.tb_lineno)
        return False

def get_create_proj_user_id(proj_id):
    try:
        db_data = session_pro.query(
            md_pro.TbUserprojBasic.User_ID,
            md_pro.TbUserprojBasic.Proj_ID,
            md_pro.TbUserprojBasic.Proj_Name,
        ).filter(
            md_pro.TbUserprojBasic.Proj_ID == proj_id
        ).first()
        if db_data:
            return dict(zip(db_data.keys(), db_data))
        else:
            return None
    except Exception as e:
        print("获取共享信息失败", e, e.__traceback__.tb_lineno)
        return False